package com.csun.dsms.utils.pt;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

public class HuangGangPtReqUtil {

    public static String httpRequest(String requestUrl,String APIKEY,String oToken){

        JSONObject jsObj = new JSONObject();
        jsObj.put("oToken", oToken);
        String jsonStrParam = jsObj.toJSONString();
        try {
            // Configure and open a connection to the site you will send the request
            URL url = new URL(requestUrl);
            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("content-type", "application/json;charset=UTF-8");
            urlConnection.setRequestProperty("x-hg-api-key", APIKEY);
            // 得到请求的输出流对象
            OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
            // 把数据写入请求的Body
            out.write(jsonStrParam);
            out.flush();
            out.close();
            // 从服务器读取响应
            InputStream inputStream = urlConnection.getInputStream();
            String encoding = urlConnection.getContentEncoding();
            String body = IOUtils.toString(inputStream, encoding);
            return body;
        } catch (IOException e) {
            String s = "{\"code\": \"-1\", \"msg\":\"接口异常\"}";
            return s;
        }
    }

    public static String httpRequestJson(String requestUrl,String APIKEY,String jsonStrParam){
        try {
            // Configure and open a connection to the site you will send the request
            URL url = new URL(requestUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("content-type", "application/json;charset=UTF-8");
            urlConnection.setRequestProperty("x-hg-api-key", APIKEY);
            // 得到请求的输出流对象
            OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
            // 把数据写入请求的Body
            out.write(jsonStrParam);
            out.flush();
            out.close();
            // 从服务器读取响应
            InputStream inputStream = urlConnection.getInputStream();
            String encoding = urlConnection.getContentEncoding();
            String body = IOUtils.toString(inputStream, encoding);
            return body;
        } catch (IOException e) {
            String s = "{\"code\": \"-1\", \"msg\":\"接口异常\"}";
            return s;
        }
    }

    public static String httpsRequestJson(String requestUrl,String APIKEY,String jsonStrParam){
        try {
            // Configure and open a connection to the site you will send the request
            URL url = new URL(requestUrl);
            HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("content-type", "application/json;charset=UTF-8");
            urlConnection.setRequestProperty("x-hg-api-key", APIKEY);
            // 得到请求的输出流对象
            OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
            // 把数据写入请求的Body
            out.write(jsonStrParam);
            out.flush();
            out.close();
            // 从服务器读取响应
            InputStream inputStream = urlConnection.getInputStream();
            String encoding = urlConnection.getContentEncoding();
            String body = IOUtils.toString(inputStream, encoding);
            return body;
        } catch (IOException e) {
            String s = "{\"code\": \"-1\", \"msg\":\"接口异常\"}";
            return s;
        }
    }


    public static String httpJson(String requestUrl,String jsonStr){
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            OutputStream os = urlConnection.getOutputStream();
            os.write(jsonStr.getBytes(StandardCharsets.UTF_8));
            os.flush();
            os.close();
            InputStream inputStream = urlConnection.getInputStream();
            String body = new BufferedReader(new InputStreamReader(inputStream))
                    .lines().collect(Collectors.joining(System.lineSeparator()));
            System.out.println(body);
            if (urlConnection.getResponseCode() == 200) {
                return body;
            } else {
                String s = "{\"code\": \"-1\", \"msg\":\"接口异常\"}";
                return s;
            }
        } catch (Exception e) {
            String s = "{\"code\": \"-1\", \"msg\":\"接口异常\"}";
            return s;
        }
    }

}
